package com.joker.web.controller.orange;

import java.util.*;

import cn.hutool.core.util.ObjectUtil;
import com.joker.common.core.domain.entity.SysDictData;
import com.joker.common.core.domain.entity.SysRole;
import com.joker.common.core.domain.entity.SysUser;
import com.joker.common.helper.LoginHelper;
import com.joker.common.utils.StreamUtils;
import com.joker.orange.domain.NovelInfo;
import com.joker.orange.service.INovelInfoService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.joker.common.annotation.RepeatSubmit;
import com.joker.common.annotation.Log;
import com.joker.common.core.controller.BaseController;
import com.joker.common.core.domain.PageQuery;
import com.joker.common.core.domain.R;
import com.joker.common.core.validate.AddGroup;
import com.joker.common.core.validate.EditGroup;
import com.joker.common.enums.BusinessType;
import com.joker.common.utils.poi.ExcelUtil;
import com.joker.orange.domain.vo.NovelChapterVo;
import com.joker.orange.domain.bo.NovelChapterBo;
import com.joker.orange.service.INovelChapterService;
import com.joker.common.core.page.TableDataInfo;

/**
 * 小说章节
 *
 * @author Joker
 * @date 2024-06-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pixiv/novelChapter")
public class NovelChapterController extends BaseController {

    private final INovelChapterService iNovelChapterService;
    private final INovelInfoService novelInfoService;

    /**
     * 查询小说章节列表
     */
    @SaCheckPermission("pixiv:novelChapter:list")
    @GetMapping("/list")
    public TableDataInfo<NovelChapterVo> list(NovelChapterBo bo, PageQuery pageQuery) {
        return iNovelChapterService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出小说章节列表
     */
    @SaCheckPermission("pixiv:novelChapter:export")
    @Log(title = "小说章节", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NovelChapterBo bo, HttpServletResponse response) {
        List<NovelChapterVo> list = iNovelChapterService.queryList(bo);
        ExcelUtil.exportExcel(list, "小说章节", NovelChapterVo.class, response);
    }

    /**
     * 获取小说章节详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pixiv:novelChapter:query")
    @GetMapping("/{id}")
    public R<NovelChapterVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iNovelChapterService.queryById(id));
    }

    /**
     * 新增小说章节
     */
    @SaCheckPermission("pixiv:novelChapter:add")
    @Log(title = "小说章节", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody NovelChapterBo bo) {
        return toAjax(iNovelChapterService.insertByBo(bo));
    }

    /**
     * 修改小说章节
     */
    @SaCheckPermission("pixiv:novelChapter:edit")
    @Log(title = "小说章节", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody NovelChapterBo bo) {
        return toAjax(iNovelChapterService.updateByBo(bo));
    }

    /**
     * 删除小说章节
     *
     * @param ids 主键串
     */
    @SaCheckPermission("pixiv:novelChapter:remove")
    @Log(title = "小说章节", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iNovelChapterService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 根据小说id获取章节信息
     *
     * @param novelId 小说ID
     */
    @SaCheckPermission("pixiv:novelChapter:query")
    @GetMapping("/batchChapter/{novelId}")
    public R<Map<String, Object>> batchChapter(@PathVariable Long novelId) {
        NovelInfo novelInfo = novelInfoService.getNovelInfo(novelId);
        List<NovelChapterVo> chapters = iNovelChapterService.selectNovelChapterByNovelId(novelId);
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("novelInfo", novelInfo);
        ajax.put("chapters", chapters);
        return R.ok(ajax);
    }

    @SaCheckPermission("pixiv:novelChapter:edit")
    @Log(title = "小说章节", businessType = BusinessType.GRANT)
    @PutMapping("/updateBatchChapter")
    public R<Void> updateBatchChapter(String chargeType, String consumeDiamond, Long[] chapterIds) {
        if (ObjectUtil.isEmpty(chargeType)) {
            return R.fail("小说收费类型为必须填写");
        }
        if ("2".equals(chargeType) && ObjectUtil.isEmpty(consumeDiamond)) {
            return R.fail("收费类型为钻石收费，消费钻石必须填写");
        }
        iNovelChapterService.updateBatchChapter(chargeType, consumeDiamond, chapterIds);
        return R.ok();
    }

}
